using System.Collections.Generic;
using Config;
using Entity.Monster;
using Map.MapObject;
using MoonFramework.FSM;
using MoonFramework.Scripts.Event;
using MoonFramework.Template;
using Tool.Name;
using UnityEngine;

namespace Entity.Player.State
{
    public class PlayerAttackState : AttackState
    {
        private Player _player => (Player)_actor;
        private readonly HashSet<BaseMapObject> _lastMapObjects = new();
        private readonly HashSet<MonsterObject> _lastMonsterObjects = new();

        public override void Enter()
        {
            base.Enter();
            _actor.viewController.AddAnimationEvent(nameof(StartPickUp), StartPickUp);
            _actor.viewController.AddAnimationEvent(nameof(StopPickUp), StopPickUp);
        }

        protected override void StartHit()
        {
            var obj = ((Player)_actor).WeaponController.CurrentWeapon.WeaponObject;
            if(!obj)return;
            obj.transform.OnTriggerEnter(CheckHitOnTriggerStay);
        }
        
        //让武器停止伤害计算
        protected override void StopHit()
        {
            var obj = ((Player)_actor).WeaponController.CurrentWeapon.WeaponObject;
            if(!obj)return;
            obj.transform.RemoveTriggerEnter(CheckHitOnTriggerStay);
            _lastMapObjects.Clear();
            _lastMonsterObjects.Clear();
        }
        
        //采摘事件开始
        private void StartPickUp()
        {
            EventCenter.Instance.Callback<ItemWeaponInfo>(EventName.Hurt, null);
        }

        private void StopPickUp()
        {
        } 

        //攻击结束
        protected override void AttackOver()
        {
            _player.fsmMachine.ChangeState(nameof(PlayerIdleState));
        }
        
        protected override async void CheckHitOnTriggerStay(Collider other, object[] arg2)
        {
            // 对方得是地图对象 / AI对象
            bool f1 = other.TryGetComponent(out BaseMapObject mapObject);
            bool f2 = other.TryGetComponent(out MonsterObject monsterObject);
            if(!f1 && !f2)return;
            
            var weaponInfo = _player.WeaponController.CurrentWeapon.WeaponData?.Config?.ItemTypeInfo as ItemWeaponInfo;
            if (mapObject)
            {
                if (!_lastMapObjects.Add(mapObject)) return;
                mapObject.Hurt(weaponInfo);
                _player.WeaponController.WeaponDamaged();
            }
            else if (monsterObject)
            {
                if (!_lastMonsterObjects.Add(monsterObject)) return;
                // GameObject effect = await GameObjPoolManager.Instance.PopAsync<GameObject>(weaponInfo.HitEffect);
                // effect.transform.position =
                //     other.ClosestPoint(_player.WeaponController.CurrentWeapon.WeaponObject.transform.position);
                monsterObject.Hurt(weaponInfo);
                _player.WeaponController.WeaponDamaged();
            }
        }

        public override void Exit()
        {
            base.Exit();
            //避免攻击状态被打断，但是伤害检测还在
            StopHit();
            _actor.viewController.RemoveAnimationEvent(nameof(StartPickUp), StartPickUp);
            _actor.viewController.RemoveAnimationEvent(nameof(StopPickUp), StopPickUp);
        }
    }
}